package routers

import (
	"MyNotesWeb/bean"
	"MyNotesWeb/data"
	"MyNotesWeb/util"
	"fmt"

	"encoding/json"

	"github.com/lunny/log"
	"github.com/lunny/tango"
	"github.com/tango-contrib/binding"
	"github.com/tango-contrib/session"
)

type Action struct {
	tango.Json
	tango.Ctx
	binding.Binder
	session.Session
}

func (this *Action) Get() interface{} {

	if list, err := data.ListTopSort(); err == nil {
		return ReJson{1, "成功", list}
	} else {
		log.Error(err)
		return ReJson{-2, "错误：" + err.Error(), nil}
	}

}

func (this *Action) GetArticle() interface{} {
	id := this.Ctx.Param(":id")
	if info, err := data.GetArticleById(util.StringToInt64(id, -1)); err == nil {
		return ReJson{1, "成功", info}
	} else {
		log.Error(err)
		return ReJson{-2, "错误：" + err.Error(), nil}
	}
}

func (this *Action) ListArticle() interface{} {

	cid := this.Ctx.Param(":id")
	if list, err := data.ListArticle(util.StringToInt64(cid, -1)); err == nil {
		return ReJson{1, "成功", list}
	} else {
		log.Error(err)
		return ReJson{-2, "错误：" + err.Error(), nil}
	}

}

func (this *Action) Post() interface{} {

	if val := this.Session.Get("UserInfo"); val != nil {
		var user bean.User = val.(bean.User)
		fmt.Println(user.Name)
		var article bean.Article
		err := this.Bind(&article)
		if err == nil {
			n, _ := data.SaveArticle(&article)
			if n > 0 {
				return ReJson{1, "添加成功！", nil}
			} else {
				return ReJson{-2, "添加失败！", nil}
			}
		} else {
			return ReJson{-1, "错误：", nil}

		}
	} else {
		return ReJson{-3, "错误：您还未登录，无法创建！", nil}
	}

}

func (this *Action) IsLogin() interface{} {

	if val := this.Session.Get("UserInfo"); val != nil {
		var user bean.User = val.(bean.User)
		return map[string]interface{}{
			"Login":    1,
			"UserName": user.Name,
		}
	} else {
		return map[string]interface{}{
			"Login":    -1,
			"UserName": "",
		}
	}

}

func (this *Action) Login() interface{} {
	if b, err := this.Ctx.Body(); err == nil {
		var dat map[string]string
		if err = json.Unmarshal(b, &dat); err == nil {
			//fmt.Printf("用户名:%s 密码:%s 密码：[%s]\n", dat["Name"], dat["Password"], util.Md5(dat["Password"]))
			user, err := data.Login(dat["Name"], util.Md5(dat["Password"]))
			if err == nil && user != nil {
				this.Session.Set("UserInfo", *user)
				return ReJson{1, "用名登录成功！", nil}
			} else {
				return ReJson{-1, "用名或密码错误！", nil}
			}
		} else {
			return ReJson{-2, "错误：数据解析失败：" + err.Error(), nil}
		}
	} else {
		return ReJson{-3, "错误：获取数据失败！", nil}
	}

}

func (this *Action) Logout() {
	this.Session.Del("UserInfo")
	this.Ctx.Redirect("/index.html")
}

func (this *Action) PushEdit() interface{} {

	if val := this.Session.Get("UserInfo"); val != nil {
		if b, err := this.Ctx.Body(); err == nil {
			fmt.Printf("传递过来的字符串:%s \n", string(b))
			var dat map[string]interface{}
			if err = json.Unmarshal(b, &dat); err == nil {
				fmt.Printf("map对象:%v 得到数字:%d \n", dat, util.ObjToInt64(dat["Cid"], 0))
				article := bean.Article{}
				article.Id = util.ObjToInt64(dat["Id"], 0)
				article.Cid = util.ObjToInt64(dat["Cid"], 0)
				article.Name = util.ObjToString(dat["Name"], "")
				article.Content = util.ObjToString(dat["Content"], "")
				n, _ := data.SaveArticle(&article)
				if n > 0 {
					return ReJson{1, "编辑成功！", &article}
				} else {
					return ReJson{-2, "编辑失败！", nil}
				}
			} else {
				return ReJson{-2, "错误：数据解析失败：" + err.Error(), nil}
			}
		} else {
			return ReJson{-3, "错误：获取数据失败！", nil}
		}
	} else {
		return ReJson{-3, "错误：您还未登录，无法创建！", nil}
	}
}
